<template>
	<view class="u-wrap">
		<!--轮播图-->
		<swiper class="swiper-container" circular :indicatorColor="indicatorColor" :indicator-dots="indicatorDots"
			:autoplay="autoplay" :interval="interval" :duration="duration">
			<swiper-item @click="clickSwiper(item)" v-for="(item,index) in swipperList" :key="index">
				<image class='imgs' :src='item.images'></image>
			</swiper-item>
		</swiper>
		<view class="tab-stricky">
			<!--搜索框-->
			<u-search @change="searchList" v-model="keywords" bg-color="#FFF" margin="8px" style="flex-grow: 1;"
				:show-action="true" action-text="搜索" :animation="true"></u-search>
		</view>
		<!--瀑布流-->
		<view class="">
			<u-waterfall v-model="flowList" ref="uWaterfall1">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load @click="toDetail(item)" threshold="-450" border-radius="15"
							:image="item.image.split(',')[0]" :index="index"></u-lazy-load>
						<view class="demo-price" @click="toDetail(item)">
							￥{{item.price}}
						</view>
						<view class="demo-tag">
							<view v-if="item.type == '0'" class="demo-tag-owner">
								闲置
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								求购
							</view>
						</view>
						<view class="demo-title" @click="toDetail(item)">
							{{item.introduce}}
						</view>
						<view class="demo-shop">
							{{item.shop}}
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index">
						<u-lazy-load @click="toDetail(item)" threshold="-450" border-radius="15"
							:image="item.image.split(',')[0]" :index="index"></u-lazy-load>
						<view class="demo-price" @click="toDetail(item)">
							￥{{item.price}}
						</view>
						<view class="demo-tag">
							<view v-if="item.type == '0'" class="demo-tag-owner">
								闲置
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								求购
							</view>
						</view>
						<view class="demo-title" @click="toDetail(item)">
							{{item.introduce}}
						</view>
						<view class="demo-shop">
							{{item.shop}}
						</view>
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>
	</view>
</template>

<script setup>
	import {
		onReady,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app';
	import {
		ref,
		toRefs
	} from 'vue'
	import {
		getIndexBannerApi,
		getIndexListApi
	} from '../../api/index.js';

	const indicatorDots = ref(true) //下方提示点
	const indicatorColor = ref("#FFF") //提示点颜色
	const autoplay = ref(true) //自动播放
	const interval = ref(5000) //自动播放间隔
	const duration = ref(500) //动画速度
	//轮播图数据
	const swipperList = ref([])
	//瀑布流
	const flowList = ref([])
	//首页轮播图
	const getBannerList = async () => {
		let res = await getIndexBannerApi()
		if (res && res.code == 200) {
			swipperList.value = res.data;
		}
	}
	//首页商品数据
	const loadStatus = ref('loadmore')
	//当前页数
	const currentPage = ref(1)
	//每页查询条数
	const pageSize = ref(10)
	//总页数
	const pages = ref(0)
	const keywords = ref('')
	const getIndexList = async () => {
		let res = await getIndexListApi({
			currentPage: currentPage.value,
			pageSize: pageSize.value,
			keywords: keywords.value
		})
		if (res && res.code == 200) {
			//设置总页数
			pages.value = res.data.pages
			flowList.value = flowList.value.concat(res.data.records);
			loadStatus.value = 'loadmore';
		}
	}
	//加载更多
	const loadMore = () => {
		// 如果当前页数大于等于总页数，状态修改为没有更多了，不再继续往下执行代码
		if (currentPage.value >= pages.value) {
			loadStatus.value = 'nomore';
			return;
		};
		loadStatus.value = 'loading'; //状态改为加载中
		currentPage.value = ++currentPage.value
		//修改页数后，重新获取数据
		getIndexList()
	}
	//触底加载
	onReachBottom(() => {
		// 如果当前页数大于等于总页数，状态修改为没有更多了，不再继续往下执行代码
		if (currentPage.value >= pages.value) {
			loadStatus.value = 'nomore';
			return;
		};
		loadStatus.value = 'loading'; //状态改为加载中
		currentPage.value = ++currentPage.value
		//修改页数后，重新获取数据
		getIndexList()
	})
	//下拉刷新
	onPullDownRefresh(() => {
		uni.reLaunch({
			url: '../index/index'
		})
	})
	//搜索
	const uWaterfall1 = ref()
	const searchList = () => {
		uWaterfall1.value.clear()
		currentPage.value = 1;
		loadStatus.value = 'loading';
		getIndexList()
	}
	//跳转详情页
	const toDetail = (item) => {
		//检查是否登录
		const test = uni.getStorageSync('userId')
		if (test) {
			uni.setStorageSync('goodsId', item.goodsId)
			if (item.type == '0') {
				uni.navigateTo({
					url: "../unused_detail/unused_detail?goods=" + JSON.stringify(item)
				})
			} else {
				uni.navigateTo({
					url: "../buy_detail/buy_detail?goods=" + JSON.stringify(item)
				})
			}
		} else {
			uni.switchTab({
				url: '../mine/mine'
			})
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			})
		}
	}
	const clickSwiper = (item) => {
		if (item.clickEvent) {
			wx.navigateTo({
				url: "pages/webview/webview?item=" + JSON.stringify(item)
			})
		}
	}
	onReady(() => {
		getBannerList()
		getIndexList()
	})
</script>

<style lang="scss">
	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.swiper-container {
		height: 190px;

		.item {
			height: 190px;
		}

		.imgs {
			height: 190px;
			width: 100%;
		}
	}

	.demo-warter {
		border-radius: 8px;
		margin: 3px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}

	.tab-stricky {
		position: sticky;
		z-index: 99;
		top: 0;
		left: 0;
		display: flex;
		align-items: center;
		background-color: #ffffff;
	}
</style>